<template>
  <div class="home">
    <!-- 首页 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="auto">
        <div class="logo"></div>
        <!-- 七 3、collapse用来控制 菜单栏的展开和折叠  router属性可以开启路由模式 vue-router index属性可以作为path进行路由跳转-->
        <el-menu
          router
          :collapse="iscollapse"
          default-active="2"
          background-color="#f9f9f9"
          class="el-menu-admin"
          @open="handleOpen"
          @close="handleClose"
        >
          <el-submenu index="1">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>用户管理</span>
            </template>
            <!-- 七 跳转到users子路由 -->
            <el-menu-item index="/users">
              <i class="el-icon-tickets"></i>
              <span>用户管理</span>
            </el-menu-item>
          </el-submenu>
          <!-- 权限列表 -->
          <el-submenu index="2">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>权限管理</span>
            </template>
            <!-- 七 跳转到users子路由 -->
            <el-menu-item index="/roles">
              <i class="el-icon-tickets"></i>
              <span>角色列表</span>
            </el-menu-item>
            <el-menu-item index="/rights">
              <i class="el-icon-tickets"></i>
              <span>权限列表</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container>
        <el-header>
          <!-- 七 4、toggle用来切换iscollapse true或false 用来切换上面侧边栏的展开和折叠 -->
          <div class="toggle-btn" @click="toggle">
            <i class="myicon myicon-menu"></i>
          </div>
          <div class="system-title">
            <span>高级电商管理系统</span>
          </div>
          <div>
            <span class="welcome">您好，{{username}}</span>
            <el-button type="warning" round @click="loginout">退出</el-button>
          </div>
        </el-header>
        <el-main>
          <!-- 七 5、创一个插槽来展示组件 -->
          <!-- 九 加入组件展示 进场和离场的动画 两个属性即可 包裹着我们的插槽-->
          <transition enter-active-class="animated fadeIn" leave-active-class="animated fadeOut">
            <router-view></router-view>
          </transition>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
// import { getUsersData } from '@/api'
export default {
  created () {
    /*     getUsersData({ query: '', pagenum: 1, pagesize: 1 }).then(res => {
      // 七 1、如果不设置请求头的token直接请求数据 会返回一个无效token给我们 但是我们很多操作都需要携带token 所以我们可以使用vue的拦截器 为请求前api的请求头添加token
      console.log(res)
    }) */
    // 七 设置用户名
    this.username = localStorage.getItem('username')
  },
  data () {
    return {
      iscollapse: false,
      username: ''
    }
  },
  methods: {
    handleOpen (key, keyPath) {
      console.log(key, keyPath)
    },
    handleClose (key, keyPath) {
      console.log(key, keyPath)
    },
    // 七 4.1切换展开个折叠的逻辑
    toggle () {
      console.log(123)
      this.iscollapse = !this.iscollapse
    },
    // 七 2、退出登录 跳转到登录页面
    loginout () {
      // 七 2.1清除token
      localStorage.clear()
      this.$router.push({ name: 'login' })
    }
  }
}
</script>

<style lang="scss" scoped>
.home {
  height: 100%;
  .el-menu-admin:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
  .el-container {
    height: 100%;
  }
  .el-aside {
    background-color: #f9f9f9;
  }
  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #409eff;
  }
  .logo {
    height: 60px;
    background: url(../assets/logo.png);
    background-size: cover;
    background-color: white;
  }
  .toggle-btn {
    padding: 0 15px;
    margin-left: -20px;
    font-size: 36px;
    color: white;
    cursor: pointer;
    line-height: 60px;
    &:hover {
      background-color: #4292cf;
    }
  }
  .system-title {
    font-size: 28px;
    color: white;
  }
  .welcome {
    color: white;
  }
}
</style>
